package com.yujiahao.bigdata.sql

import org.apache.spark.rdd.RDD
import org.apache.spark.sql.{DataFrame, Dataset, Row, SparkSession}

object SparkSQL01_DataFrame_DataSet {
  def main(args: Array[String]): Unit = {
    //TODO 1、SparkSQL直接构建无法构建，包是私有的，使用伴生对象伴生类构建builder
    //SparkSQL对SparkCore进行了封装，所以环境也要发生变化

    val spark = SparkSession.builder
      .master("local")
      .appName("Word Count")
      .getOrCreate()

    import spark.implicits._
    // RDD DataFrame DataSet之间的转换
    //构建RDD
    val rdd: RDD[(Int, String, Int)] = spark.sparkContext.makeRDD(List(
      (1, "liudehua", 50), (2, "zhangxueyou", 60), (3, "wangzuxian", 30)))

    //todo 将RDD转换成DataFrame
    val df: DataFrame = rdd.toDF("id", "name", "age")
    df.show()

    //todo 将DataFrame转换成RDD
    val rdd1: RDD[Row] = df.rdd

    //todo 将DataFrame 转换成DataSet但是这里缺少类型，那就先增加一个类型
    val ds: Dataset[User] = df.as[User]

    //TODO 2、关闭资源
    spark.stop()

  }

  //也是通过样例类的方式进行创建
  case class User(id: Int, name: String, age: Int)
}
